class Shop::FareModesController < ShopController
  before_action :set_fare_template

  def index
    @fare_modes = @fare_template.fare_modes.order("created_at DESC").page(params[:page]).per(params[:per])
  end
  
  def show
    @fare_mode = FareMode.find params[:id]
  end

  def new
    @fare_mode = FareMode.new
  end

  def create
    @fare_mode = @fare_template.fare_modes.build fare_mode_params
    if @fare_mode.save
      redirect_to shop_fare_template_fare_modes_path(@fare_template), notice: "保存成功！"
    else
      redirect_to shop_fare_template_fare_modes_path(@fare_template), notice: "保存失败！"
    end
  end

  def edit
    @fare_mode = FareMode.find params[:id]
  end

  def update
    @fare_mode = FareMode.find params[:id]
    if @fare_mode.update fare_mode_params
      redirect_to shop_fare_template_fare_modes_path(@fare_template), notice: "更新成功！"
    else
      redirect_to shop_fare_template_fare_modes_path(@fare_template), notice: "更新失败！"
    end
  end
  
  def destroy
    @fare_mode = FareMode.find params[:id]
    if @fare_mode.destroy
      redirect_to shop_fare_template_fare_modes_path(@fare_template), notice: "删除成功."
    else
      redirect_to shop_fare_template_fare_modes_path(@fare_template), notice: "删除失败."
    end
  end

  private

  def set_fare_template
    @fare_template = FareTemplate.find params[:fare_template_id]
  end

  def fare_mode_params
    params.require(:fare_mode).permit(:min_distance, :max_distance, :min_weight, :max_weight, :first_n_piece, :first_amount, :second_n_piece, :second_amount, province_ids: [])
  end
end
